#!/usr/bin/env bash
#
# Summary: Set or show the global PHP version and update the Apache apxs
# library link
#
# Usage: phpenv global <version>
#        phpenv global --unset
#
# Sets the global PHP version. You can override the global version at
# any time by setting a directory-specific version with `phpenv local'
# or by setting the `PHPENV_VERSION' environment variable.
#
# <version> should be a string matching a PHP version known to phpenv.
# The special version string `system' will use your default system PHP.
# Run `phpenv versions' for a list of available PHP versions.

set -e
[ -n "$PHPENV_DEBUG" ] && set -x

# Provide phpenv completions
if [ "$1" = "--complete" ]; then
  echo system --unset
  exec phpenv-versions --bare
fi

PHPENV_VERSION="$1"
PHPENV_VERSION_FILE="${PHPENV_ROOT}/version"

if [ "$1" == "--unset" ]; then
  rm -f "$PHPENV_VERSION_FILE"
  PHPENV_VERSION=""
fi

if [ -n "$PHPENV_VERSION" ]; then
  phpenv-version-file-write "$PHPENV_VERSION_FILE" "$PHPENV_VERSION"
else
  PHPENV_VERSION=$(phpenv-version-file-read "$PHPENV_VERSION_FILE") ||
  PHPENV_VERSION=$(phpenv-version-file-read "${PHPENV_ROOT}/global") ||
  PHPENV_VERSION=$(phpenv-version-file-read "${PHPENV_ROOT}/default") ||
  PHPENV_VERSION=system
fi

echo ${PHPENV_VERSION}
